package com.henu.rescue.controller;

import com.henu.rescue.entity.Station;
import com.henu.rescue.entity.Telephone;
import com.henu.rescue.entity.VO.StaIdName;
import com.henu.rescue.entity.VO.StationVO;
import com.henu.rescue.service.StaService;
import com.henu.rescue.service.TelService;
import com.henu.rescue.utils.Page;
import com.henu.rescue.utils.Result;
import com.henu.rescue.utils.ResultEnums;
import com.henu.rescue.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RestController
@RequestMapping("station/")
@Api("服务点信息管理controller")
@Slf4j
public class StaController {

    @Autowired
    private StaService staService;

    @Autowired
    private TelService telService;

    @ApiOperation("添加服务点")
    @PostMapping("addSta")
    public Result addSta(@RequestBody Station station){
        System.out.println(station);
        if(station == null){
            return new Result(ResultEnums.ADD_FAILED,"车辆救援服务点添加失败！");
        }
        String staLocation = station.getStaLocation();
        // 定义正则表达式
        String regex = "\\((-?\\d+\\.?\\d*),(-?\\d+\\.?\\d*)\\)";

        // 使用正则表达式进行匹配
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(staLocation);

        // 判断是否匹配成功
        if (matcher.matches()) {
            // staLocation 符合 (double,double) 格式
            // 执行业务逻辑
            Integer integer = staService.addSta(station);
            if(integer == 1){
                return new Result(ResultEnums.ADD_SUCCESS,"车辆救援服务点添加成功！");
            }else{
                return new Result(ResultEnums.ADD_FAILED,"车辆救援服务点添加失败！");
            }
        } else {
            // staLocation 不符合格式
            return new Result(ResultEnums.PARAMS_ERROR, "staLocation 不符合 (double,double) 格式");
        }
    }

    @ApiOperation("删除服务点")
    @DeleteMapping("delSta/{staId}")
    public Result delSta(@PathVariable Integer staId){

        if(staId == null){
            return new Result(ResultEnums.DEL_ERROR,"车辆救援服务点删除失败！");
        }
        //将救援服务点电话删除
        Integer count = telService.delTelFromOrg(2, staId);
        /*if(count != 1){
            return new Result(ResultEnums.DEL_ERROR,"电话删除失败");
        }*/
        staService.delSta(staId);
        return new Result(ResultEnums.SUCCESS,"车辆救援服务点删除成功！");

    }

    @ApiOperation("修改服务点")
    @PostMapping("updateSta")
    public Result updateSta(@RequestBody Station station){

        if(station == null){
            return new Result(ResultEnums.PARM_ERROR,"车辆救援服务点id错误！");
        }
        //获取当前修改完后的station对应的的电话实体
        Telephone telephone = new Telephone();
        telephone.setTelType(2);
        telephone.setHostId(station.getStaId());
        telephone.setTelNumber(station.getStaTel());
        Telephone byTypeHostId = telService.selectByTypeHostId(telephone.getTelType(), telephone.getHostId());
       /* System.out.println(byTypeHostId);
        System.out.println(telephone);*/
        //判断电话号码是否被修改过
        System.out.println(telephone);
        System.out.println(byTypeHostId);
        if(!telephone.getTelNumber().equals(byTypeHostId.getTelNumber())){
            byTypeHostId.setTelNumber(telephone.getTelNumber());
            telService.updateTel(byTypeHostId);
        }
        staService.update(station);
        return new Result(ResultEnums.SUCCESS,"车辆救援服务点信息更新成功！");

    }

    @ApiOperation("分页获取服务点")
    @PostMapping("getAllByPage")
    public Result<Page<StationVO>> getAllByPage(@RequestBody Page<StationVO> page){
        page.setSortMethod("desc");
        String sortColumn = page.getSortColumn();
        if (StringUtils.isNotBlank(sortColumn)) {
            // 排序列不为空
            String[] sortColumns = {"sta_id"};
            List<String> sortList = Arrays.asList(sortColumns);
            if (!sortList.contains(sortColumn.toLowerCase())) {
                return new Result<>(ResultEnums.PARAMS_ERROR.getCode(), "排序参数不合法！");
            }
        }
        page= staService.selectStaInfoByPage(page);
        return new Result(ResultEnums.QUERY_SUCCESS,page);
    }

    @ApiOperation("获取所有服务点的name和id")
    @PostMapping("getAll")
    public Result getAll(){
        List<Station> stations = staService.selectAll();
        HashMap<Integer, String> staIdName = new HashMap<>();
        //只要id和name属性
        for (int i = 0; i < stations.size(); i++) {
            Station old = stations.get(i);
            staIdName.put(old.getStaId(), old.getStaName());
        }
        return new Result(ResultEnums.QUERY_SUCCESS,staIdName);
    }

}
